$import( "SmitearWebGL.Objects.Canvas.ScreenQuad" );
$import( "SmitearWebGL.Programs.LightPrePass.SpecularProgram" );

SmitearWebGL.Objects.Canvas.LightPrePass.Specular = SmitearWebGL.Objects.Canvas.ScreenQuad.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Objects.Canvas.LightPrePass.Specular" );
		
		this._pointLightingTech = null;
		this._dirLightingTech = null;
		this._spotLightingTech = null;
	}
	,
	
	//override render function
	renderCallBack : function( renderSession, lights )
	{
		if ( ! lights || lights.length == 0 ) return SmitearWebGL.Core.HResult.SUCCESS;
			
		var i = 0;
		var program;
		
		this.context.gl.blendFunc( this.context.gl.ONE, this.context.gl.ONE );
		this.context.gl.blendEquation( this.context.gl.FUNC_ADD );
		this.context.gl.enable( this.context.gl.BLEND );
		this.context.gl.depthMask( false );
		this.context.gl.disable( this.context.gl.CULL_FACE );
		
		for ( i = 0; i < lights.length; ++ i )
		{
			switch ( lights[i].type )
			{
				case SmitearWebGL.Objects.Light.L_POINT : 
					program = this._pointLightingTech;
					break;
					
				case SmitearWebGL.Objects.Light.L_DIR : 
					program = this._dirLightingTech;
					break;
					
				case SmitearWebGL.Objects.Light.L_SPOT : 
					program = this._spotLightingTech;
					break;
			}
			
			var hr = SmitearWebGL.Core.HResult.SUCCESS;
			
			hr |= program.activeShader();
			hr |= program.activeUniform( renderSession, this );
			hr |= program.activeLightUniform( lights[i] );
			
			hr |= this._vertexBuffer.bind();
			hr |= this._vertexBuffer.active( program.getShader() );
			
			this.context.gl.drawArrays( this.context.gl.TRIANGLE_STRIP, 0, 4 );
		}
		
		this.context.gl.disable( this.context.gl.BLEND );
		this.context.gl.depthMask( true );
		
		return hr;
	}
	,
	_createResource : function( params )
	{
		this._super();
		
		this._pointLightingTech = this.context.programManager.getResourceByName( SmitearWebGL.Programs.InternalPrograms.Type.LP_POINT_LIGHT_SPECULAR );
		this._dirLightingTech = this.context.programManager.getResourceByName( SmitearWebGL.Programs.InternalPrograms.Type.LP_DIRECT_LIGHT_SPECULAR );
		this._spotLightingTech = this.context.programManager.getResourceByName( SmitearWebGL.Programs.InternalPrograms.Type.LP_SPOT_LIGHT_SPECULAR );
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		this._pointLightingTech = null;
		this._dirLightingTech = null;
		this._spotLightingTech = null;
	}
}
);
